<?php
class Server {
  public static function autocomplete($orm, $sys, $inst, $is_iframe) {
    $req = Utils::get_param('term', '');
    
    $opts = new stdClass();
    $opts->cd = 't.track_id LIKE :v1';
    $opts->cdv = array(':v1' => '%' . $req . '%');
    $rslt = $orm->get('pb_colg_enrollment', 't.track_id', 'ASC', 1, SIZE, $opts)->d;
    $res = array();
    foreach ($rslt as $itm) {
      $obj = new stdClass();
      $obj->label = $itm->track_id;
      $obj->value = $itm->track_id;
      $res[] = $obj;
    }
    echo json_encode($res);
  }
  
  public static function autocomplete2($orm, $sys, $inst, $is_iframe) {
    $req = Utils::get_param('term', '');
    
    $opts = new stdClass();
    $opts->cd = 't.title__ LIKE :v1';
    $opts->cdv = array(':v1' => '%' . $req . '%');
    $rslt = $orm->get('pb_colg_student', 't.title__', 'ASC', 1, SIZE, $opts)->d;
    $res = array();
    foreach ($rslt as $itm) {
      $obj = new stdClass();
      $obj->label = $itm->title__;
      $obj->value = $itm->title__;
      $res[] = $obj;
    }
    echo json_encode($res);
  }
  
  public static function search($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.track_id LIKE :v1 AND t.student_title__ LIKE :v2';
    $opts->cdv = array(':v1' => '%' . $req->term . '%', ':v2' => '%' . $req->term2 . '%');
    $res = $orm->get('pb_colg_enrollment', $req->o, $req->q, $req->pg, SIZE, $opts);
    echo json_encode($res);
  }
  
  public static function submit_enrl($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    if ($req->enrolled_program_id == 0) {
      $opts = new stdClass();
      $opts->cd = 't.program_id = :v1 AND t.student_id = :v2 AND t.status != 0 AND t.program_type != :v3';
      $opts->cdv = array(':v1' => $req->program_id, ':v2' => $req->student_id, ':v3' => PT_LANGUAGE);
      $rslt = $orm->get('pb_colg_enrolled_program', NULL, NULL, NULL, NULL, $opts)->d;
      if (count($rslt) == 0) {
        $obj = new stdClass();
        $obj->program_id = $req->program_id;
        $obj->program_name = $req->program_name;
        $obj->program_type = $req->program_type;
        $obj->student_id = $req->student_id;
        $orm->add('pb_colg_enrolled_program', $obj);
        $obj->id = $orm->last_id();
        $req->enrolled_program_id = $obj->id;

        if ($obj->program_type == PT_LANGUAGE) {
          $obj2 = new stdClass();
          $obj2->enrollment_id = $req->id;
          $obj2->enrolled_program_id = $obj->id;
          $obj2->student_id = $req->student_id;
          $obj2->intake_id = 0;
          $obj2->start = $req->start_academic;
          $obj2->end = $req->end_academic;
          $orm->add('pb_colg_enrolled_intake', $obj2);
        }
      } else {
        $req->enrolled_program_id = $rslt[0]->id;
      }
    }
    $req->is_paid = 1;
    $req->is_cancel = 0;
    $orm->update('pb_colg_enrollment', $req);
    if (isset($req->__fees)) {
      foreach ($req->__fees as $itm) {
        $orm->add('pb_colg_invoice', $itm);
        if ($itm->__paid > 0 || $itm->__discount > 0) {
          $itm->invoice_id = $orm->last_id();
          $itm->invoice_track_id = $itm->track_id;
          $itm->amount = $itm->__paid;
          $itm->discount = $itm->__discount;
          unset($itm->balance__);
          unset($itm->track_id);
          unset($itm->due_date);
          $orm->add('pb_colg_payment', $itm);
        }
      }
      unset($req->__fees);
    }

    $req->balance__ = ColgUtils::bal_enrl($orm, $req->id);
    ColgUtils::bal_student($orm, $req->student_id);
    echo json_encode($req);
  }
  
  public static function clearup($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $req->is_paid = 0;
    $req->enrolled_program_id = 0;
    $orm->update('pb_colg_enrollment', $req);
    $opts = new stdClass();
    $opts->cd = 'enrollment_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $orm->delete_by_opts('pb_colg_invoice', $opts);
    $orm->delete_by_opts('pb_colg_payment', $opts);
    require_once '../include/colg/colgutils.class.php';
    $req->balance__ = ColgUtils::bal_enrl($orm, $req->id);
    ColgUtils::bal_student($orm, $req->student_id);
    echo json_encode($req);
  }
  
  public static function get_fees($orm, $sys, $inst, $is_iframe) {
    $req = Utils::get_param('req', 0);
    
    $opts = new stdClass();
    $opts->cd = 't.enrollment_id = :v1';
    $opts->cdv = array(':v1' => $req);
    $invs = $orm->get('pb_colg_invoice', NULL, NULL, NULL, NULL, $opts)->d;
    $pays = $orm->get('pb_colg_payment', NULL, NULL, NULL, NULL, $opts)->d;
    foreach ($invs as $itm) {
      $itm->__paid = 0;
      $itm->__discount = 0;
      foreach ($pays as $itm2) {
        if ($itm2->invoice_track_id == $itm->track_id) {
          $itm->__paid += $itm2->amount;
          $itm->__discount += $itm2->discount;
        }
      }
    }
    echo json_encode($invs);
  }
}
?>